<?php
include_once ("ez_sql.php");
//user Setting
define("DB_USER", $DBUser);         // <-- mysql db user
define("DB_PASSWORD", $DBPassword);     // <-- mysql db password
define("DB_NAME", $DBName);     // <-- mysql db name
//define("DB_NAME2", $DBName2);       // <-- mysql db name
define("DB_HOST", $DBHost); // <-- mysql server host



class mydb {

    function mydb($dbuser, $dbpassword, $dbname, $dbhost) {
        $DBConn = new db($dbuser, $dbpassword, $dbname, $dbhost);
        $this->dbc = $DBConn;
    }

    function add_htmlfield($table, $dbfield, $htmlfield) {
        $nextindex = count($this->Fields);
        if (!$nextindex) $nextindex=0;
        $this->Tables[$nextindex] = $table;
        $this->Fields[$nextindex] = array("$dbfield"=>"$htmlfield");
    }

    function add_data($table, $dbfield, $htmlfield, $isNumber=0) {
        $nextindex = count($this->Fields);
        if (!$nextindex) $nextindex=0;
        $this->Table = $table;
        if ($isNumber) $htmlfield = str_replace(",",$htmlfield);
        $this->Fields[$nextindex]["field"] = $dbfield;
        $this->Fields[$nextindex]["html"]  = $htmlfield;
    }

    function AddData($Field,$Data) {
        $this->MyFields[$Field] = $Data;
    }

    function InsertData($TableName) {
        foreach ($this->MyFields as $Field=>$Data) {
            if (strlen($FieldStr))  $FieldStr .= ",$Field";
            else $FieldStr = $Field;
            if (strlen($DataStr))   $DataStr .= ",\"$Data\"";
            else $DataStr = "\"$Data\"";
        }
        $SQL = "INSERT INTO $TableName ($FieldStr) VALUES ($DataStr) ";
        $this->dbc->query($SQL);
        unset($this->MyFields);
    }

    function parent_key($fromtable, $relation_key) {
        $this->ParentTable = $fromtable;
        $this->ParentKey = $relation_key;
        $this->ParentKeyValue = "";
    }

    function insert() {
        for ($x=0;$x<count($this->Tables);$x++) {
            if ($CurrentTable<>$this->Tables[$x]) {
                if (strlen($CurrentTable)) {
                    if (strlen($this->ParentKeyValue)) {
                        $SQL = "INSERT INTO $CurrentTable ($Fields,".$this->ParentKey.") VALUES ($Values,".$this->ParentKeyValue.")";
                        $this->dbc->query($SQL);
//                      $this->dbc->debug();
                        $this->Fields=""; $this->Tables=""; $this->PrimaryKey="";
                    } elseif ($this->ParentKey) {
                        $this->ParentKeyValue = $this->dbc->get_var("SELECT max(".$this->ParentKey.") FROM ".$this->ParentTable);
                        $SQL = "INSERT INTO $CurrentTable ($Fields,".$this->ParentKey.") VALUES ($Values,".$this->ParentKeyValue.")";
                        $this->dbc->query($SQL);
//                      $this->dbc->debug();
                        $this->Fields=""; $this->Tables=""; $this->PrimaryKey="";
                    } else print("<br><b>Application Error:</b> Parent Key not exist<br><br>");
                }
                list ($dbfield, $htmlfield)=each($this->Fields[$x]);
                $Fields = $dbfield;
                $Values = "\"".addslashes($htmlfield)."\"";
                $CurrentTable=$this->Tables[$x];
            } else {
                list ($dbfield, $htmlfield)=each($this->Fields[$x]);
                $Fields .= ",".$dbfield;
                $Values .= ",\"".addslashes($htmlfield)."\"";
            }

        }
        $SQL = "INSERT INTO $CurrentTable ($Fields) VALUES ($Values)";
        $this->dbc->query($SQL);
//      $this->dbc->debug();
        $this->Fields=""; $this->Tables=""; $this->PrimaryKey="";
    }

    function update($PVALUE="") {
		//echo "update masuk sini"; exit;
        $primary_key   = PRIMARY_KEY;
        if (strlen($PVALUE)) $primary_value = $PVALUE;
        else $primary_value = GetParam(PRIMARY_KEY,"");

        for ($x=0;$x<count($this->Tables);$x++) {
            if ($CurrentTable<>$this->Tables[$x]) {
                if (strlen($CurrentTable)) {
                    $SQL = "UPDATE $CurrentTable SET $SetField WHERE $primary_key=\"$primary_value\"";
                    $this->dbc->query($SQL);
//                  $this->dbc->debug();
                    $this->Fields=""; $this->Tables=""; $this->PrimaryKey="";
                }
                list ($dbfield, $htmlfield)=each($this->Fields[$x]);

                $SetField  = $dbfield."=\"".addslashes($htmlfield)."\"";
                $CurrentTable=$this->Tables[$x];
            } else {
                list ($dbfield, $htmlfield)=each($this->Fields[$x]);
                $SetField .= ",".$dbfield."=\"".addslashes($htmlfield)."\"";
            }
        }
        $SQL = "UPDATE $CurrentTable SET $SetField WHERE $primary_key=\"$primary_value\"";
		//Edited by Krisna Wednesday 26 October 2010
        //echo $SQL; exit;
		
        $this->dbc->query($SQL);
//      $this->dbc->debug();
        $this->Fields=""; $this->Tables=""; $this->PrimaryKey="";
    }

    function setWhere($StringWhere) {
        $this->Where = "$StringWhere";
    }

    function updateGrid($keyfield, $value) {
        $primary_key   = $keyfield;
        $primary_value = $value;
        for ($x=0;$x<count($this->Fields);$x++) {
                $dbfield = $this->Fields[$x]["field"];
                $htmlfield = $this->Fields[$x]["html"];
                if ($SetField) $SetField .= ",".$dbfield."=\"".addslashes($htmlfield)."\"";
                else $SetField = $dbfield."=\"".addslashes($htmlfield)."\"";
        }
        if (strlen($this->Where)) $SQL = "UPDATE ".$this->Table." SET $SetField WHERE ".$this->Where;
        else $SQL = "UPDATE ".$this->Table." SET $SetField WHERE $primary_key=\"$primary_value\"";
        //echo "$SQL"; exit;
		$this->dbc->query($SQL);
//      $this->dbc->debug();
        unset($this->Fields);
        unset($this->Table);
    }

    function insertGrid() {
		
				
        for ($x=0;$x<count($this->Fields);$x++) {
            $dbfield = $this->Fields[$x]["field"];
            $htmlfield = $this->Fields[$x]["html"];
            
            if (!strlen($Flds)) {
					$Flds   = $dbfield;
					$Values = "\"".addslashes($htmlfield)."\"";
            } else {
					$Flds   .= ",".$dbfield;
					$Values .= ",\"".addslashes($htmlfield)."\"";
            }
        }
                
        
        if(!empty($Flds))
        {
			$SQL = "INSERT INTO ".$this->Table." ($Flds) VALUES ($Values)";        
   	                //echo $SQL."<br><br>";exit;
			$this->dbc->query($SQL);
	        //$this->dbc->debug();
			unset($this->Fields);
			unset($this->Table);
	    }
    }
	
	function insertGridUpdate($primary_key, $primary_value) {
		//echo "test"; exit;
				
		for ($x=0;$x<count($this->Fields);$x++) {
                $dbfield = $this->Fields[$x]["field"];
                $htmlfield = $this->Fields[$x]["html"];
				
				//if ($SetField) $SetField .= ",".$dbfield."=\"".addslashes($htmlfield)."\"";
                //else $SetField = $dbfield."=\"".addslashes($htmlfield)."\"";
				
				if (!strlen($SetField)) $SetField .= ",".$dbfield."=\"".addslashes($htmlfield)."\"";
                else $SetField = $dbfield."=\"".addslashes($htmlfield)."\"";
				
                if (!strlen($Flds)) {
					if(strlen($dbfield)){
						$Flds   = $dbfield."=\"".addslashes($htmlfield)."\"";
						$Values = "\"".addslashes($htmlfield)."\"";
					}
				} else {
					$Flds   .= ",".$dbfield."=\"".addslashes($htmlfield)."\"";
					$Values .= ",\"".addslashes($htmlfield)."\"";
				}
        }        
        
        //if(!empty($Flds))
        //{
			$SQL = "UPDATE ".$this->Table." SET $Flds WHERE $primary_key=\"$primary_value\"";
   	                //echo $SQL."<br><br>"; exit;
			$this->dbc->query($SQL);
	        //$this->dbc->debug();
			unset($this->Fields);
			unset($this->Table);
	    //}
    }

    function  clearData() {
        unset($this->Fields);
        unset($this->Table);
    }

    function deleteGrid($table,$key,$param_key) {
        $SQL = "DELETE FROM $table WHERE ".$key."='".GetParam("$param_key","")."' AND ktrasl=".GetOfficeID();
        $this->dbc->query($SQL);
        //$this->dbc->debug();
        unset($this->Fields);
        unset($this->Table);
    }

    function delete($table,$primary_key) {		
        $SQL = "DELETE FROM $table WHERE ".PRIMARY_KEY."='".GetParam("$primary_key","")."'";        
        $this->dbc->query($SQL);
        $this->Fields=""; $this->Tables=""; $this->PrimaryKey="";
    }

    function QueryNoResult ($query, $Conn="") {
        global $DBMysql;
        if (!strlen($Conn)) mysql_query($query,$DBMysql);
        else mysql_query($query,$Conn);
    }

    function GetResSQL ($query, $Conn="") {
        global $DBMysql;
        if (!strlen($Conn)) $res = mysql_query($query,$DBMysql);
        else {$res = mysql_query($query,$Conn);}
        if ($res)
        $row = mysql_fetch_row ($res);
        return $row[0];
    }

    function GetConnection1 () {
        $DBMysql = mysql_connect(DB_HOST,DB_USER,DB_PASSWORD) ;
        mysql_select_db(DB_NAME,$DBMysql);
        return  $DBMysql;
    }
/*
    function GetConnection2 () {
        $DBMysql2 = mysql_connect(DB_HOST,DB_USER,DB_PASSWORD) ;
        mysql_select_db(DB_NAME2,$DBMysql2);
        return  $DBMysql2;
    }
*/
}


$DBConnection   = new mydb(DB_USER, DB_PASSWORD, DB_NAME, DB_HOST);

$DBMysql = mysql_connect(DB_HOST,DB_USER,DB_PASSWORD) ;
mysql_select_db($DBApp,$DBMysql);
/*
$DBMysql2 = mysql_connect(DB_HOST,DB_USER,DB_PASSWORD) ;
mysql_select_db(DB_NAME2,$DBMysql2);
*/
?>
